$(function () {

    var store_id = $("select[name='store_id'] option:selected").val();
    var class_base_numbering_id = $("select[name='class_base_numbering_id'] option:selected").val();

    if(typeof(class_base_numbering_id) == 'undefined')class_base_numbering_id=0;

    select_value(store_id, class_base_numbering_id);

});

$("select[name='store_id']").change(function () {

    select_value($(this).val(), 0);

});

$("select[name='class_base_numbering_id']").change(function () {

    var store_id = $("select[name='store_id'] option:selected").val();

    select_value(store_id, $(this).val());

});

function select_value(store_id = 0, class_base_numbering_id = 0) {

    var form = new FormData();

    form.append("store_id", store_id);
    form.append("class_base_numbering_id", class_base_numbering_id);
    //form.append("base_time_id", base_time_id);
    //form.append("week_id", week_id);

    var class_base_html = '<option value="">請選擇課程</option>';
    var teacher_html = '<option value="">請選擇老師</option>';
    var classroom_html = '<option value="">請選擇課室</option>';

    var loadding_index = layer.load(1, {
        shade: [0.1,'#fff'] //0.1透明度的白色背景
    });

    $.ajax({
        type: 'POST',
        url: '/class_base_teacher',
        dataType: "json",   //返回格式为json
        data: form,
        processData: false,
        contentType: false,
        success: function (result) {

            layer.close(loadding_index);

            var base = result.data.base;
            var teacher = result.data.teacher;
            var classroom = result.data.classroom;

            for (var i = 0; i < base.length; i++) {
                class_base_html += '<option value="' + base[i].id + '" ';
                if (base[i].id == class_base_id) class_base_html += 'selected';
                class_base_html += '>' + base[i].class_name + '</option>';
            }

            $("select[name='class_base']").html(class_base_html);

            for (var i = 0; i < teacher.length; i++) {
                teacher_html += '<option value="' + teacher[i].id + '" ';
                if (teacher[i].id == teacher_id) teacher_html += 'selected';
                teacher_html += '>' + teacher[i].teacher_name + '</option>';
            }

            $("select[name='teacher']").html(teacher_html);

            for (var i = 0; i < classroom.length; i++) {
                classroom_html += '<option value="' + classroom[i].id + '" ';
                if (classroom[i].id == classroom_id) classroom_html += 'selected';
                classroom_html += '>' + classroom[i].numbering + '</option>';
            }

            $("select[name='classroom']").html(classroom_html);

        }
    });

}
